package icu.funkye.confine.wrapper;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import icu.funkye.confine.limiter.product.Limiter;

/**
 * @author jianbin.chen
 */
public class ResourceWrapper {

    private final String resourceMethod;

    private Limiter rateLimiter;

    private final LimitMode limitType;

    private int waitTime = 0;

    private List<Long> rtList = new CopyOnWriteArrayList<>();

    /**
     * 阈值rt
     */
    private Long maxRt = 0L;

    /**
     * 平均rt,单位毫秒
     */
    private Long averageRt = 0L;

    public ResourceWrapper(String resourceMethod, Limiter rateLimiter, LimitMode limitType) {
        this.resourceMethod = resourceMethod;
        this.rateLimiter = rateLimiter;
        this.limitType = limitType;
    }

    public ResourceWrapper(String resourceMethod, LimitMode limitType) {
        this.resourceMethod = resourceMethod;
        this.limitType = limitType;
    }

    public String getResourceMethod() {
        return resourceMethod;
    }

    public LimitMode getLimitType() {
        return limitType;
    }

    public int getWaitTime() {
        return waitTime;
    }

    public void setWaitTime(int waitTime) {
        this.waitTime = waitTime;
    }

    public List<Long> getRtList() {
        return rtList;
    }

    public void addRt(Long rt) {
        this.rtList.add(rt);
    }

    public Long getAverageRt() {
        return averageRt;
    }

    public void setAverageRt(Long averageRt) {
        this.averageRt = averageRt;
    }

    public Limiter getRateLimiter() {
        return rateLimiter;
    }

    public Limiter setRateLimiter(Limiter rateLimiter) {
        this.rateLimiter = rateLimiter;
        return this.rateLimiter;
    }

    public Long getMaxRt() {
        return maxRt;
    }

    public ResourceWrapper setMaxRt(Long maxRt) {
        this.maxRt = maxRt;
        return this;
    }

    public enum LimitMode {
        DROP, // 丢弃
        WAIT // 等待
    }

}
